package leetcode.simple._9;

/**
 * @author mgl
 * @version 1.0
 * @description 判断一个数是否为回文数
 * @date 2023/7/7 21:42
 */
public class Palindrome {

    public boolean isPalindrome(int x) {
        if (x < 0) return false;

        int digit = 0;
        int reverse = 0;
        int temp = x;

        while(temp>0){
            digit = temp % 10;
            reverse = 10 * reverse + digit;
            temp /= 10;
        }

        return reverse == x;
    }

    //将数字转为字符串
    public boolean isPalindrome01(int x) {
        String str = String.valueOf(x);
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            int j = chars.length - i - 1;

            if (chars[i] != chars[j]) return false;
        }

        return true;
    }
}
